using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Bosphorus.Integration.Jira.Thor.Client.PlugIn.Core.Forms;
using Bosphorus.Integration.Jira.Thor.Client.PlugIn.Common.Configuration;
using Bosphorus.Integration.Jira.Thor.Client.PlugIn.JiraIntegrationService;
using Bosphorus.Integration.Jira.Thor.Client.PlugIn.View.Domain;

namespace Bosphorus.Integration.Jira.Thor.Client.PlugIn.Core.Forms
{
    public partial class Issues : Form
    {
        public event EventHandler<OkClickedEventArgs> OkClicked;
        public event EventHandler<EventArgs> CancelClicked;

        private readonly string projectKey;

        public Issues(string projectKey)
        {
            InitializeComponent();
            this.projectKey = projectKey;

            OkClicked = delegate { };
            CancelClicked = delegate { };
        }

        protected override void OnLoad(EventArgs e)
        {
            IssueController issueController = new IssueController();
            gridView.DataSource = issueController.GetIssueViewModelByProject(projectKey);
        }

        private void OnOkClicked(object sender, EventArgs e)
        {
            OkClickedEventArgs okClickedEventArgs = new OkClickedEventArgs();
            IList<IssueViewModel> issueViewModels = (IList<IssueViewModel>)gridView.DataSource;
            foreach (IssueViewModel issueViewModel in issueViewModels)
            {
                okClickedEventArgs.ClosedIssue.Add(issueViewModel);
            }

            OkClicked(this, okClickedEventArgs);
            this.Close();
        }

        private void OnCancelClicked(object sender, EventArgs e)
        {
            this.Close();
            CancelClicked(this, EventArgs.Empty);
        }
    }
}